﻿using blackhouse.WebNemo.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace blackhouse.WebNemo.Business.Groups {
    public class NemoRecordFileOperation : RecordFileOperation {

        #region Fields

        private Factory factory = Factory.Instance();
        private IDataAccess data;

        #endregion

        #region Properties

        private IDataAccess DataLayer {
            get {
                if (this.data == null)
                    this.data = this.factory.GetDataAccess();
                return this.data;
            }
        }

        #endregion

        #region Constructors

        public NemoRecordFileOperation(IGroupRecord record, string column)
            : base(record, column) { }

        #endregion

        #region Methods

        public override bool SaveFile(RecordFileData data, Stream fileStream) {
            this.DataLayer.SaveGroupRecordFile(this.record.BelongsToGroupId, this.record.RecordId, this.column, data.FileName, fileStream, data.Size, this.factory.GetCurrentUser().UserId);
            return true;
        }

        public override byte[] GetFileData() {
            return this.DataLayer.GetFileStream(this.column, this.record.BelongsToGroupId, this.record.RecordId);
        }

        public override RecordFileData GetInfo() {
            FileData data = this.DataLayer.GetFileInfo(this.column, this.record.BelongsToGroupId, this.record.RecordId);
            RecordFileData fileData = new RecordFileData();
            if (data != null && !String.IsNullOrEmpty(data.FileName)) {
                fileData.Exist = true;
                fileData.FileName = data.FileName;
                fileData.Hash = data.Hash;
                fileData.Size = data.FileSize;
            }
            return fileData;
        }

        #endregion

    }
}
